<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>店员管理</title>
	<meta name="decorator" content="default"/>
</head>
<body>
	<div class="icon-buttons">
		<input type="button" class="leave-clean-session" data-inline="true" data-icon="back" data-iconpos="notext" value="返回"/>
	</div>
	<form:form id="searchForm" modelAttribute="employee" action="${ctx}/employee/employee/" method="post">
		<input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
		<input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
		<div class="middleCls">
			<input type="button" value="添加员工" data-inline="true" onclick="bossPasswordPopup('${ctx}/employee/employee/form')">
			<fieldset data-role="controlgroup" data-type="horizontal" data-inline="true">
		        <input type="radio" name="status" id="radio-choice-a" value="0" ${empty status || status == 0 ? "checked" : ""}>
		        <label for="radio-choice-a">在职</label>
		        <input type="radio" name="status" id="radio-choice-b" value="1" ${status == 1 ? "checked" : ""}>
		        <label for="radio-choice-b">离职</label>
			</fieldset>
			<label>姓名 ：</label>
			<form:input type="search" path="name" htmlEscape="false" maxlength="50" data-inline="true"/>
			<input id="btnSubmit" type="submit" value="查询" data-inline="true"/>
		  	<input id="btnImport" type="button" data-inline="true" value="导入"/>
			<input id="btnExport" type="button" data-inline="true" value="导出"/>
		</div>
	</form:form>
	<tags:message content="${message}"/>
	<div  class="form-input">
		<table id="contentTable" data-role="table" class="ui-responsive table-stroke">
			<thead><tr><th>名称</th><th>性别</th><th>职位</th><th>电话</th><th>状态</th><th></th></tr></thead>
			<tbody>
			<c:forEach items="${page.list}" var="employee">
				<tr id="${employee.id}" status="${employee.status}">
					<td>${employee.name}</td>
					<td>${fns:getDictLabel(employee.gender, 'gender', '')}</td>
					<td>${employee.storeRole.name}</td>
					<td>${employee.mobile}</td>
					<td>${employee.status == 0 ? '在职':'离职'}</td>
					<td class="operateTD" id="${employee.id}">
						<input type="button" data-inline="true" data-icon="arrow-d" data-iconpos="notext">
					</td>
				</tr>
			</c:forEach>
			</tbody>
		</table>
		<div class="pagination">${page}</div>
	</div>
	<script type="text/javascript">
		$(document).ready(function() {
			$(".operateTD").click(function(){
				operateMenu(this.id);
			});
			$("#contentTable tbody tr").click(function(e){
				if (!this.id) {
					return;
				}
				sessionStorage.parentPageNo = $("#pageNo").val();
				$("#contentTable tbody tr").removeClass("select_active");
				$(this).addClass("select_active");
				var url = "${ctx}/employee/employee/detail?id=" + this.id;
				window.location.href = url;
			});
			$("body").click(function(e){
				$(".ui-icon-arrow-u").removeClass("ui-icon-arrow-u").addClass("ui-icon-arrow-d");
				$(".opreationTR").removeClass("opreationTR");
				$(".opreation").remove();
			});
		 	
		 	$(document).delegate('.employeePanel', 'click', function() {
		 		bossPasswordPopup('${ctx}/employee/employee/detail?id=' + this.id);
			});
			if(global.isMobile()) {
				$("#btnImport").parent().css("display", "none");
				$("#btnExport").parent().css("display", "none");
			} else {
				$("#btnExport").click(function(){
			 		global.confirm("确认要导出员工数据吗？", exportExcel);
				});
			 	$("#btnImport").click(function(){
					global.uploadDialog("员工资料上传", employeeUpload, employeeTemplate);
				});
			}
			var currentSelectId = '${currentSelectId}';
			if(currentSelectId){
				$("#"+currentSelectId).addClass("opreationTR");
			} else {
				$("#contentTable tbody tr:first-child").addClass("opreationTR");
			}
		});
		var item;
		function operateMenu(id){
			item = $("#opreation-"+ id);
			$(".opreationTR").removeClass("opreationTR");
			$(".ui-icon-arrow-u").removeClass("ui-icon-arrow-u").addClass("ui-icon-arrow-d");
			if(item.length > 0) {
				item.remove();
				this.event.stopPropagation();
				return;
			}
			$("#"+id + " .ui-icon-arrow-d").removeClass("ui-icon-arrow-d").addClass("ui-icon-arrow-u");
			$(".opreation").remove();
			var status = $("#" + id).attr("status");
			var url = "${ctx}/employee/employee/detail?id=" + id;
			var html= "<tr id='opreation-" + id + "' class='opreation'><td colspan='6'>";
			html += "<a data-role='button' data-inline='true' href='" + url+ "'>明细</a>";
				if(status == "0"){
					html += "<a class='menu-del' data-role='button' data-inline='true' href='#'>离职</a></td>";
				}
			html += "</tr>";
			var del = function(){
				var url = "${ctx}/employee/employee/status?id=" + id;
				bossLoginDeletePopup("确定要将此员工的状态改为离职？", url);
			};
			
			$(".menu-del").die().live('click',del);
			$("#" + id).addClass("opreationTR");
			$("#" + id).after(html);
			$("#contentTable").trigger("create");
			this.event.stopPropagation();
		}
		function employeeUpload(){
			if(beforeUpload() == false){
				return;
			}
			$.ajaxFileUpload({
					url:'${ctx}/employee/employee/import',
					secureuri:false,
					fileElementId:'fileToUpload',
					dataType: 'json',
					beforeSend:function()
					{
						$("#loading").show();
					},
					complete:function()
					{
						$("#loading").hide();
					},				
					success: function (data, status)
					{
						var msg = "导入员工信息完成：<br/>";
						if (data.successNum && data.failureNum){
							var snum = parseInt(data.successNum);
							var fnum = parseInt(data.failureNum);
							msg += "<h5>您要导入的文件中总计有" + (snum + fnum) + "条数据,导入成功" + snum + "条,失败" + fnum + "条</h5><br/>";
						}
						if (parseInt(data.failureNum) > 0) {
							msg += "失败信息如下：<br/><h5>";
							var errorArr = data.errorMsg.split(";");
							for (var i = 0, len = errorArr.length; i < len; i ++) {
								msg += errorArr[i] + "<br/>";
							}
							msg += "</h5>";
						}
						global.alert(msg);
					},
					error: function (data, status, e)
					{
						showTip(e);
					}
				}
			);
            return false;
		}
		function employeeTemplate(){
			var url = "${ctx}/employee/employee/import/template";
			window.location.href = url;
		}
		function exportExcel(){
			var url = "${ctx}/employee/employee/export?name="+ $("#name").val() + "&status=" + $('input:radio[name=status]:checked').val();
			window.location.href = url;
		}
		function page(n,s){
			$("#pageNo").val(n);
			$("#pageSize").val(s);
			$("#searchForm").submit();
        	return false;
        }
	</script>
</body>
</html>
